/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.util;

import org.apache.log4j.Logger;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import java.io.*;
import java.util.HashMap;

public class SelfReader {
    private static final Logger logger = Logger.getLogger(SelfReader.class);
    private static final HashMap<String, String> selfMap = new HashMap<String, String>();

    public static HashMap<String, String> read()
    {
        try
        {
            if (selfMap.isEmpty())
            {
                logger.info("Reading wihidum config from file system.");
                File file = new File("WihidumConfig.xml");
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document doc = documentBuilder.parse(file);
                doc.getDocumentElement().normalize();

                Element wihidumElement = doc.getDocumentElement();

                NodeList topologyNameList = wihidumElement.getElementsByTagName("Topology");
                Element topologyNameElement = (Element) topologyNameList.item(0);
                NodeList topologyNameNodeList = topologyNameElement.getChildNodes();
                String topologyName = topologyNameNodeList.item(0).getNodeValue();
                selfMap.put("topology", topologyName);

                NodeList mySelfDataList = wihidumElement.getElementsByTagName("MySelf");
                for (int i = 0; i < mySelfDataList.getLength(); i++) {
                    Node node = mySelfDataList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE && node != null) {
                        Element mySelfElement = (Element) node;

                        NodeList listID = mySelfElement.getElementsByTagName("ID");
                        Element idElement = (Element) listID.item(0);
                        NodeList id = idElement.getChildNodes();
                        selfMap.put("id", id.item(0).getNodeValue());

                        NodeList listAddress = mySelfElement.getElementsByTagName("Address");
                        Element addressElement = (Element) listAddress.item(0);
                        NodeList address = addressElement.getChildNodes();
                        selfMap.put("address", address.item(0).getNodeValue());

                        NodeList listRole = mySelfElement.getElementsByTagName("Role");
                        Element roleElement = (Element) listRole.item(0);
                        NodeList role = roleElement.getChildNodes();
                        selfMap.put("role", role.item(0).getNodeValue());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  
        }
        finally
        {
            return selfMap;
        }
    }
}
